package com.tydic.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.system.domain.ContractTemplate;
import com.tydic.system.domain.req.ContractTemplateByPageRequest;
import com.tydic.system.domain.req.ContractTemplateRequest;
import com.tydic.system.mapper.ContractTemplateMapper;
import com.tydic.system.service.IContractTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ContractTemplateServiceImpl implements IContractTemplateService {
    private final ContractTemplateMapper baseMapper;

    /**
     * 添加合同模板
     *
     * @param contractTemplateRequest
     * @return
     */
    @Override
    public Boolean addContractTemp(ContractTemplateRequest contractTemplateRequest) {
        ContractTemplate contractTemplate = BeanUtil.toBean(contractTemplateRequest, ContractTemplate.class);
        contractTemplate.setTemplateCode(IdUtil.getSnowflakeNextIdStr());
        return baseMapper.insert(contractTemplate) > 0;
    }

    private LambdaQueryWrapper<ContractTemplate> buildQueryWrapper(ContractTemplate bo) {
        LambdaQueryWrapper<ContractTemplate> lqw = Wrappers.lambdaQuery(ContractTemplate.class);
        lqw.eq(CharSequenceUtil.isNotBlank(bo.getTemplateCode()), ContractTemplate::getTemplateCode, bo.getTemplateCode());
        lqw.like(CharSequenceUtil.isNotBlank(bo.getTemplateName()), ContractTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(CharSequenceUtil.isNotBlank(bo.getTemplateStatus()), ContractTemplate::getTemplateStatus, bo.getTemplateStatus());
        lqw.eq(CharSequenceUtil.isNotBlank(bo.getTemplateType()), ContractTemplate::getTemplateType, bo.getTemplateType());
        return lqw;
    }

    /**
     * 查询合同模板列表
     *
     * @param request
     * @return
     */
    @Override
    public Page<ContractTemplate> queryTempList(ContractTemplateByPageRequest request) {
        return baseMapper.selectPage(request.build(), buildQueryWrapper(BeanUtil.toBean(request, ContractTemplate.class)));
    }


    /**
     * 更改合同状态
     *
     * @param templateCode
     * @return
     */
    @Override
    public boolean change(String templateCode, String status) {
        return baseMapper.update(Wrappers.lambdaUpdate(ContractTemplate.class).set(ContractTemplate::getTemplateStatus, status).eq(ContractTemplate::getTemplateCode, templateCode)) > 0;
    }

    /**
     * 更新
     *
     * @param contractTemplateRequest
     * @return
     */
    @Override
    public Boolean updateContractTemp(ContractTemplateRequest contractTemplateRequest) {
        ContractTemplate contractTemplate = BeanUtil.toBean(contractTemplateRequest, ContractTemplate.class);
        return baseMapper.updateById(contractTemplate) > 0;
    }

    @Override
    public ContractTemplate getTemplateInfo(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<ContractTemplate> queryList(ContractTemplate contractTemplate) {
        return baseMapper.selectList(buildQueryWrapper(contractTemplate));
    }
}
